﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
//using HalconLib;
using CommonTools;
using UserCtrl;
using MotionIoLib;
using System.Threading.Tasks;
using System.Diagnostics;
using BaseDll;
using UserData;
using System.IO;
using System.Windows.Forms.DataVisualization.Charting;
using OtherDevice;
using System.Drawing.Imaging;
using LogType = UserData.LogType;
using ModuleCapture;
using Sunny.UI;
using VisionBaseLib;
using HY_UPHMgr;
using StationCommunication;
using static StationDemo.Form_Auto;
using System.IO.Ports;
using System.Text;
using System.Runtime.InteropServices.ComTypes;

namespace StationDemo
{
    public partial class Form_Auto : UIPage
    {

       
        public delegate void EventShowSN(string sn);//刷新退料状态
        public static EventShowSN ShowSN;
       
        public delegate void EventFlashResult(string strStep);//
        public static EventFlashResult FlashResult;

        public Form_Auto()
        {
            InitializeComponent();
        }
        private void Function_遍历控件(Control control)
        {

            foreach (var item in control.Controls)
            {
                if(item is Interface_UserControl)
                {
                    ((Interface_UserControl)item).Init();
                }
                else
                {
                    Function_遍历控件((Control)item);
                }
                
            }


        }
        private void Form_Auto_Load(object sender, EventArgs e)
        {
            #region 初始化参数配置

            Function_遍历控件(this);

          
            ShowSN += showSN;
            FlashResult += flashResult;
            //专案信息加载
            label_CurrentFile.Text = "当前产品:" + ParamSetMgr.GetInstance().CurrentProductFile;
            txt_机型.Text = Param_System.Instance.机型._strParamVal;
            txt_批次.Text = Param_System.Instance.批次._strParamVal;
            txt_操作者.Text = Param_System.Instance.操作者._strParamVal;
            txt_设备编号.Text = Param_System.Instance.设备编号._strParamVal;
            txt_工站编号.Text = Param_System.Instance.工站编号._strParamVal;

       

         
          

            Form_NGControl form_NGB = new Form_NGControl(Station.三角测量站.ToString());
            form_NGB.Dock = DockStyle.Fill;
            form_NGB.Show();
            tableLayoutPanel_NG项统计.Controls.Add(form_NGB);
            
            #endregion
            timer1.Interval = 100;
            timer1.Start();
        }
      
        private void btn_修改机型_Click(object sender, EventArgs e)
        {
            string value = "";
            string desc = "请输入机型名称：".Trim();
            if (UIInputDialog.InputStringDialog(this, ref value, true, desc))
            {
                if (value != "")
                {
                    txt_机型.Text = value;
                    Param_System.Instance.机型._strParamVal = value;
                    ParamSetMgr.GetInstance().SaveParam(Param_System.Instance);
                }
                else
                {
                    UIMessageBox.ShowError("请输入正确的机型");
                    return;
                }
            }
        }
        private void btn_修改批次_Click(object sender, EventArgs e)
        {
            string value = "";
            string desc = "请输入批次名称：".Trim();
            if (UIInputDialog.InputStringDialog(this, ref value, true, desc))
            {
                if (value != "")
                {
                    txt_批次.Text = value;
                    Param_System.Instance.批次._strParamVal = value;
                    ParamSetMgr.GetInstance().SaveParam(Param_System.Instance);
                }
                else
                {
                    UIMessageBox.ShowError("请输入正确的批次");
                    return;
                }
            }

        }
        private void btn_修改操作者_Click(object sender, EventArgs e)
        {
            string value = "";
            string desc = "请输入操作者名称：".Trim();
            if (UIInputDialog.InputStringDialog(this, ref value, true, desc))
            {
                if (value != "")
                {
                    txt_操作者.Text = value;
                    Param_System.Instance.操作者._strParamVal = value;
                    ParamSetMgr.GetInstance().SaveParam(Param_System.Instance);
                }
                else
                {
                    UIMessageBox.ShowError("请输入正确的操作者");
                    return;
                }
            }
        }
        private void btn_修改设备编号_Click(object sender, EventArgs e)
        {
            string value = "";
            string desc = "请输入设备编号名称：".Trim();
            if (UIInputDialog.InputStringDialog(this, ref value, true, desc))
            {
                if (value != "")
                {
                    txt_设备编号.Text = value;
                    Param_System.Instance.设备编号._strParamVal = value;
                    ParamSetMgr.GetInstance().SaveParam(Param_System.Instance);
                }
                else
                {
                    UIMessageBox.ShowError("请输入正确的设备编号");
                    return;
                }
            }
        }
        private void btn_修改工站编号_Click(object sender, EventArgs e)
        {
            string value = "";
            string desc = "请输入工站编号名称：".Trim();
            if (UIInputDialog.InputStringDialog(this, ref value, true, desc))
            {
                if (value != "")
                {
                    txt_工站编号.Text = value;
                    Param_System.Instance.工站编号._strParamVal = value;
                    ParamSetMgr.GetInstance().SaveParam(Param_System.Instance);
                }
                else
                {
                    UIMessageBox.ShowError("请输入正确的工站编号");
                    return;
                }
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                SeverLinkState();
            }
            catch (Exception)
            {

            }
        }
        private void SeverLinkState()
        {
            if (LoadSeverMgr.Instance.server.Function_IsConnect())
            {
                btn_通讯状态.FillColor = Color.Green;
            }
            else
            {
                btn_通讯状态.FillColor = Color.Red;
            }

           

        }
        private void showSN(string SN)
        {
            if (InvokeRequired)
            {
                this.BeginInvoke(new Action(() => showSN(SN)));

            }
            else
            {
                txt_SN.Text = SN;
            }


        }
        private void txt_SN_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                UserConfig.str_SN = this.txt_SN.Text;
                UserConfig.b_isScan = true;
            }
        }
        private void txt_SN_Load(object sender, EventArgs e)
        {

        }

        private void flashResult(string strStep)
        {
            if (InvokeRequired)
            {
                this.BeginInvoke(new Action(() => flashResult(strStep)));
            }
            else
            {
                label_Result.Text = $"{strStep}";
                label_Result.ForeColor = strStep == "Pass" ? Color.Green : Color.Red;

            }

        }
    }
}
